package com.qf.fc.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qf.fc.dto.MoodDetail;
import com.qf.fc.dto.MoodNum;
import com.qf.fc.entity.Mood;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 动态表(Mood)表数据库访问层
 *
 * @author makejava
 * @since 2024-09-23 23:34:47
 */
public interface MoodDao extends BaseMapper<Mood> {

    @Select("select m.*,ifnull(ml.likes,0) likes,ud.imageurl,ud.nickname from t_mood m left join (select count(*) likes,mid from t_mood_like group by mid) ml on m.id=ml.mid inner join t_user_detail ud on ud.uid=m.uid where m.uid=#{uid} order by m.create_time desc")
    List<MoodDetail> selectByUid(Integer uid);

    @Select("")
    MoodDetail selectByid(Integer id);

    @Select("select m.*,ifnull(ml.likes,0) likes,ud.imageurl,ud.nickname from t_mood m left join (select count(*) likes,mid from t_mood_like group by mid) ml on m.id=ml.mid inner join t_user_detail ud on ud.uid=m.uid order by ${order} desc limit #{start},#{size}")
    List<MoodDetail> selectLimit(@Param("order") String order, @Param("start")Integer start, @Param("size")Integer size);

    @Select("select count(*) from t_mood")
    Long selectCount();

    @Select("SELECT \n" +
            "    IFNULL(mcc.clikes, 0) AS clikes, \n" +
            "    IFNULL(mcc.comments, 0) AS comments, \n" +
            "    m.read_num AS readNum, \n" +
            "    m.id, \n" +
            "    ud.imageurl \n" + // 添加用户图像字段
            "FROM \n" +
            "    t_mood m\n" +
            "LEFT JOIN \n" +
            "    (\n" +
            "        SELECT \n" +
            "            COUNT(*) AS comments, \n" +
            "            mc.mid, \n" +
            "            SUM(mc.clikes) AS clikes \n" +
            "        FROM \n" +
            "            (\n" +
            "                SELECT \n" +
            "                    c.id, \n" +
            "                    c.mid, \n" +
            "                    cl.clikes \n" +
            "                FROM \n" +
            "                    t_comment c\n" +
            "                LEFT JOIN \n" +
            "                    (\n" +
            "                        SELECT \n" +
            "                            COUNT(*) AS clikes, \n" +
            "                            cid \n" +
            "                        FROM \n" +
            "                            t_comment_like \n" +
            "                        GROUP BY \n" +
            "                            cid\n" +
            "                    ) cl \n" +
            "                ON c.id = cl.cid\n" +
            "            ) mc \n" +
            "        GROUP BY \n" +
            "            mc.mid\n" +
            "    ) mcc \n" +
            "ON m.id = mcc.mid\n" +
            "JOIN t_user_detail ud ON m.uid = ud.uid \n" + // 添加 t_user_detail 表的连接
            "WHERE \n" +
            "    m.id = #{id}")
    MoodNum selectMid(Integer id);

    @Update("update t_mood SET read_num = read_num + 1 WHERE id=#{id}")
    int updateReads(Integer id);

    @Select("select * from t_mood where uid=#{uid}")
    List<MoodDetail> selectByMyUid(String uid);
}

